class_name Point extends Node2D

var kong:Texture2D = preload("res://游戏/core/落点/图片/空地.png")
var shan:Texture2D = preload("res://游戏/core/落点/图片/山脉.png")
var shui:Texture2D = preload("res://游戏/core/落点/图片/江水.png")
var lin:Texture2D = preload("res://游戏/core/落点/图片/树林.png")
var sha:Texture2D = preload("res://游戏/core/落点/图片/沙漠.png")
var jing:Texture2D = preload("res://游戏/core/落点/图片/水晶.png")

var arr_terrain:Dictionary = {0:kong,1:shan,2:shui,3:lin,4:sha,5:jing}
var real_terrain:int = 0
var fake_terrain:int = 0
var x:float = 0 
var y:float = 0 
var id:String
var is_view = false
var is_waitingClick = false
var arr_marks:Array = []	#{"type":mark.type, "player":mark.big.player.username}

var small:Small = null
var big:Big = null

func _ready():
	visible = false
	$"六边形外圈/无视野遮挡".visible = false
	$"选中提示".visible = false
	await get_tree().create_timer(4).timeout
	if ObjectManager.gameManager.me_player:
		$"选中提示".self_modulate = ObjectManager.gameManager.me_player.color

func init(point:Dictionary):
	real_terrain = point["terrain"]
	changeTerrain(real_terrain)
	x = point["position"]["x"]
	y = point["position"]["y"]
	position = Vector2(x*87,-y*75)
	id = GameUtils.getPointId(x,y)
	ObjectManager.gameManager.map_points.merge({id:self})
	await get_tree().create_timer((abs(x)+abs(y))*0.1).timeout
	visible = true
	$"AnimationPlayer".play("降落")

# 只有地形变化后才会执行
func update(data:Dictionary):
	real_terrain = data.terrain
	arr_marks = data.arr_marks

# 地形显示变化，不代表真实地形
func changeTerrain(terrain:int):
	fake_terrain = terrain
	$"六边形外圈/六边形内圈/Sprite2D".texture = arr_terrain[fake_terrain]

# 是否有自己的印记
func hasMyMark(type:String):
	var player = ObjectManager.gameManager.me_player
	for mark in arr_marks:
		if mark.type == type && mark.username == player.username:
			return true
	return false

# 是否有别人的印记
func hasOthersMark(type:String):
	var player = ObjectManager.gameManager.me_player
	for mark in arr_marks:
		if mark.type == type && mark.username != player.username:
			return true
	return false

# 改变视野
func refreshView():
	if is_view:
		$"六边形外圈/无视野遮挡".visible = false
		if small!=null:
			$"六边形外圈".color = small.player.color
		else:
			$"六边形外圈".color = Color.DARK_GRAY
		changeTerrain(real_terrain)
	else:
		$"六边形外圈/无视野遮挡".visible = true
		$"六边形外圈".color = Color.BLACK

# 被棋子占据时，地图外框改变颜色
func changeColor(color:Color):
	if color != $"六边形外圈".color:
		$"六边形外圈".color = color

func _on_mouse_entered():
	if !GameUtils.getIsCanClickPoint():
		return
	if is_waitingClick:
		$"选中提示".visible = true
		return
	$AnimationPlayer.play("默认")
	$"AnimationPlayer".queue("鼠标指向")
	if small != null:
		small.on_mouse_entered()
	elif big != null:
		big.on_mouse_entered()

func _on_mouse_exited():
	if !GameUtils.getIsCanClickPoint():
		return
	if is_waitingClick:
		$"选中提示".visible = false
		return
	$"AnimationPlayer".play("鼠标移开")
	if small != null:
		small.on_mouse_exited()
	elif big != null:
		big.on_mouse_exited()


var position_pressed = Vector2(0,0)
func _on_input_event(_viewport:Node, event:InputEvent, _shape_idx:int):
	if !GameUtils.getIsCanClickPoint():
		return
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_LEFT:
			if event.pressed:
				position_pressed = event.position
			else :
				if event.position.distance_to(position_pressed) < 10:
					click()
		elif event.button_index == MOUSE_BUTTON_RIGHT && event.pressed:
			rightClick()

# 点击进入选中状态、棋子放大
func click():
	$AnimationPlayer.play("默认")
	ObjectManager.gameManager.operationManager.selectPoint(self)

# 右键点击，直接切到棋子位置，弹出棋子放大版
func rightClick():
	$AnimationPlayer.play("默认")
	ObjectManager.gameManager.operationManager.rightSelect(self)

func default():
	is_waitingClick = false
	$AnimationPlayer.play("默认")
	$"选中提示".visible = false

# 落点进入待点击状态
func waitClick():
	is_waitingClick = true
	$AnimationPlayer.play("默认")
	$AnimationPlayer.queue("待点击")


func _on_animation_started(anim_name):
	if anim_name == "默认":
		if small != null:
			small.on_mouse_exited()
		elif big != null:
			big.on_mouse_exited()
